package com.xy.mainp.main.widget;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;

import com.xy.mainp.R;
import com.xy.mainp.ViewTabItemBinding;


/**
 * Created by John on 2016/10/28.
 */

public class TabItemView extends LinearLayout {
    private int[] imgs;
    private ViewTabItemBinding binding;

    public TabItemView(Context context) {
        this(context, null);
    }

    public TabItemView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TabItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.view_tab_item, this, true);
    }

    @Override
    public void setSelected(boolean selected) {
        super.setSelected(selected);
        if (imgs.length != 2) {
            return;
        }
        if (isSelected()){
            binding.imgTabBarItme.setImageResource(imgs[1]);
            binding.tvTabBarItemName.setVisibility(VISIBLE);
        }else {
            binding.imgTabBarItme.setImageResource(imgs[0]);
            binding.tvTabBarItemName.setVisibility(GONE);
        }
    }

    public void setImgs(int[] imgs) {
        this.imgs = imgs;
        if (imgs.length != 2) {
            return;
        }
        if (isSelected()){
            binding.imgTabBarItme.setImageResource(imgs[1]);
        }else {
            binding.imgTabBarItme.setImageResource(imgs[0]);
        }
    }

    public void setName(String name) {
        binding.tvTabBarItemName.setText(name);
    }
}
